<template>
  <div class="register">
    <el-form ref="resetRef" :model="resetForm" :rules="resetRules" class="register-form">
      <div class="title-box">
        <h3 class="title">{{ title }}</h3>
        <lang-select />
      </div>
      <el-form-item prop="userName">
        <el-input v-model="resetForm.userName" type="text" size="large" auto-complete="off" :placeholder="proxy.$t('passwordreset.userName')">
          <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="nickName">
        <el-input v-model="resetForm.nickName" type="text" size="large" auto-complete="off" :placeholder="proxy.$t('passwordreset.nickName')">
          <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="idCardNumber">
        <el-input v-model="resetForm.idCardNumber" type="text" size="large" auto-complete="off" :placeholder="proxy.$t('passwordreset.idCardNumber')">
          <template #prefix><svg-icon icon-class="idCard" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="phonenumber">
        <el-input v-model="resetForm.phonenumber" type="text" size="large" auto-complete="off" :placeholder="proxy.$t('passwordreset.phonenumber')">
          <template #prefix><svg-icon icon-class="phone" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="contact">
        <el-input v-model="resetForm.contactNumber" type="text" size="large" auto-complete="off" :placeholder="proxy.$t('passwordreset.contactNumber')">
          <template #prefix><svg-icon icon-class="phone" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button :loading="loading" size="large" type="primary" style="width: 100%" @click.prevent="handleReset">
          <span v-if="!loading">{{ proxy.$t('passwordreset.resetPassword') }}</span>
          <span v-else>{{ proxy.$t('passwordreset.resettingPassword') }}</span>
        </el-button>
        <div style="float: right">
          <router-link class="link-type" :to="'/login'">{{ proxy.$t('passwordreset.switchLoginPage') }}</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>忘记密码</span>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElMessageBox } from 'element-plus';
import { resetUserPassword } from '@/api/login'; // 假设存在该 API 用于重置密码
import { PasswordResetForm } from '@/api/types'; // 假设存在该类型定义
import { to } from 'await-to-js';
import { useI18n } from 'vue-i18n';


const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const title = import.meta.env.VITE_APP_TITLE;
const router = useRouter();

const { t } = useI18n();

const resetForm = ref<PasswordResetForm>({
  userName: '',
  nickName: '',
  idCardNumber: '',
  phonenumber: '',
  contactNumber: ''
});

const resetRules: ElFormRules = {
  username: [
    { required: true, trigger: 'blur', message: t('passwordreset.rule.username.required') },
    { min: 2, max: 20, message: t('passwordreset.rule.username.length', { min: 2, max: 20 }), trigger: 'blur' }
  ],
  name: [
    { required: true, trigger: 'blur', message: t('passwordreset.rule.name.required') },
    { min: 2, max: 20, message: t('passwordreset.rule.name.length', { min: 2, max: 20 }), trigger: 'blur' }
  ],
  idCardNumber: [
    { required: true, trigger: 'blur', message: t('passwordreset.rule.idCardNumber.required') },
    { pattern: /^\d{17}[\dXx]$/, message: t('passwordreset.rule.idCardNumber.pattern'), trigger: 'blur' }
  ],
  phoneNumber: [
    { required: true, trigger: 'blur', message: t('passwordreset.rule.phoneNumber.required') },
    { pattern: /^1[3-9]\d{9}$/, message: t('passwordreset.rule.phoneNumber.pattern'), trigger: 'blur' }
  ],
  emergencyPhoneNumber: [
    { required: true, trigger: 'blur', message: t('passwordreset.rule.emergencyPhoneNumber.required') },
    { pattern: /^1[3-9]\d{9}$/, message: t('passwordreset.rule.emergencyPhoneNumber.pattern'), trigger: 'blur' }
  ]
};

const loading = ref(false);
const resetRef = ref<ElFormInstance>();

const handleReset = () => {
  resetRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true;
      const [err, result] = await to(resetUserPassword(resetForm.value));
      debugger;
      // 修正HTML标签和移除无效属性
      if (result) {
        await ElMessageBox.alert(
          `<span style="color: red;">${t('passwordreset.resetSuccess')}</span>`,
          '系统提示',
          {
            dangerouslyUseHTMLString: true,
            type: 'success'
          }
        );
        await router.push('/login');
      } else {
        await ElMessageBox.alert(
          `<span style="color: red;">${t('passwordreset.resetfault')}</span>`,
          '系统提示',
          {
            dangerouslyUseHTMLString: true,
            type: 'error'
          }
        );
      }
      
      // 统一在所有分支结束后重置loading状态
      loading.value = false;
    }
  });
};

onMounted(() => {
  // 可以在这里添加初始化逻辑
});
</script>

<style lang="scss" scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url('../assets/images/login-background.jpg');
  background-size: cover;
}

.title-box {
  display: flex;

  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
  }

  :deep(.lang-select--style) {
    line-height: 0;
    color: #7483a3;
  }
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 40px;

    input {
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0;
  }
}

.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial, serif;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>